package cn.itcast.nems.product.scheduler;

import cn.itcast.nems.common.bean.ProductState;
import cn.itcast.nems.product.dao.dataobject.BizNo;
import cn.itcast.nems.product.dao.dataobject.ProductDO;
import cn.itcast.nems.product.dao.dataobject.TuitionFeeDO;
import cn.itcast.nems.product.dao.dataobject.TuitionFeePhaseDO;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author liyong
 * create: 2023-09-04 13:45:02
 **/
public class PriceChangeProductCal extends ProductPriceCal {

    private final List<ProductDO> result = new ArrayList<>();

    public PriceChangeProductCal(List<ProductDO> products,
                                 List<TuitionFeeDO> tuitionFees,
                                 Map<Integer, List<TuitionFeePhaseDO>> phases,
                                 Map<Integer, Set<String>> schools) {
        super(products, tuitionFees, phases, schools);
    }

    public List<ProductDO> calculate() {
        super.products.forEach(product -> {
            BizNo bizNo = product.getBizNoObject();
            final Integer phaseCount = super.clazzPhaseCounts.get(bizNo.getClazzId());
            TuitionFeeDO tuitionFee = super.findTuitionFee(product, phaseCount);
            if (tuitionFee == null) {
                logger.warn("产品{}没有找到价格配置", product.getBizNo());
                this.calProductState(product, false);
            } else {
                LocalDate useDate = product.getEffectiveDatetime();
                if (TuitionFeeDO.TIME_DIMENSION_SING_UP.equalsIgnoreCase(tuitionFee.getTimeDimension())) {
                    useDate = LocalDate.now();
                }
                if (phaseCount == 1) {
                    tuitionFee = super.findTuitionFee(product, phaseCount, useDate);
                    if (tuitionFee == null) {
                        logger.warn("产品{}没有找到价格配置", product.getBizNo());
                        this.calProductState(product, false);
                    } else {
                        this.calProductState(product, true);
                    }
                } else {
                    final TuitionFeePhaseDO tuitionFeePhase = super.findTuitionFeePhase(product, phaseCount, useDate);
                    if (tuitionFeePhase == null) {
                        logger.warn("产品{}没有找到阶段价格配置", product.getBizNo());
                        this.calProductState(product, false);
                    } else {
                        this.calProductState(product, true);
                    }
                }
            }
        });
        return result;
    }

    private void calProductState(ProductDO product, boolean foundPrice) {
        if (!foundPrice && ProductState.VALID.toString().equals(product.getState())) {
            product.setState(ProductState.PENDING.toString());
            result.add(product);
        }
        if (foundPrice && ProductState.PENDING.toString().equals(product.getState())) {
            product.setState(ProductState.VALID.toString());
            result.add(product);
        }
    }

    public List<ProductDO> getResult() {
        return result;
    }
}
